package com.xyd.validate;

import com.xyd.entity.Transaction;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class TransactionValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        //判断是否为Transaction类，如果是，进行验证
        return Transaction.class.equals(aClass);
    }

    @Override
    public void validate(Object obj, Errors errors) {
        Transaction trans = (Transaction) obj;
        //总价=单价*数量
        double dis = trans.getAmount() - (trans.getPrice() * trans.getQuantity());
        if (Math.abs(dis)>0.01){
            errors.rejectValue("amount",null,"交易金额有误差");
        }
    }
}
